/*
 * Licensed under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.submarine.config;

import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.Properties;

import org.submarine.common.Constants;
import org.submarine.common.Logger;

/**
 * 
 * @file org.submarine.config.ConfigUtil.java
 *
 *  配置文件读取工具类
 * 
 * @author gaojian
 * @created 2013-8-17 下午11:08:40
 * @version 0.0.1
 * @since 0.0.1
 *
 */
public class ConfigUtil {
	
	private static final Logger logger = Logger.getLogger(ConfigUtil.class);
	
	private static volatile Properties PROPERTIES;
	
	private ConfigUtil() {}
	
	public static String getProperty(String key) {
		return PROPERTIES.getProperty(key);
	}
	
	public static String getProperty(String key, String defaultValue) {
		return PROPERTIES.getProperty(key, defaultValue);
	}
	
	static {
		PROPERTIES = new Properties();
		String fileName = Constants.PROPERTIES_FILE;
        if (fileName.startsWith("/")) {
            try {
                FileInputStream input = new FileInputStream(fileName);
                try {
                	PROPERTIES.load(input);
                } finally {
                    input.close();
                }
            } catch (Throwable e) {
                logger.error("Failed to load " + fileName + " file from " + fileName + "(ingore this file): " + e.getMessage(), e);
            }
        }
        
        List<java.net.URL> list = new ArrayList<java.net.URL>();
        try {
            Enumeration<java.net.URL> urls = ConfigUtil.class.getClassLoader().getResources(fileName);
            list = new ArrayList<java.net.URL>();
            while (urls.hasMoreElements()) {
                list.add(urls.nextElement());
            }
        } catch (Throwable t) {
            logger.error("Fail to load " + fileName + " file: " + t.getMessage(), t);
        }
        
        if (list.size() > 0) {
        	logger.info("load " + fileName + " properties file from " + list);
        	for (java.net.URL url : list) {
        		try {
        			Properties p = new Properties();
        			InputStream input = url.openStream();
        			if (input != null) {
        				try {
        					p.load(input);
        					PROPERTIES.putAll(p);
        				} finally {
        					try {
        						input.close();
        					} catch (Throwable t) {}
        				}
        			}
        		} catch (Throwable e) {
        			logger.error("Fail to load " + fileName + " file from " + url + "(ingore this file): " + e.getMessage(), e);
        		}
        	}
        }
        
    }
	
}
